import {defineStore} from 'pinia'

interface ILoginJump {
    url?: string;
    type?: string;
}

export const useAdminStore = defineStore('admin', {
    state: () => {
        return {
            admin: {},//登陆者的身份
            rulesTree: [],//登陆者的权限规则
            rules: {},//登陆者的权限规则
            deptsTree: [],//登陆者的管理部门
            depts: {},//登陆者的管理部门
            loginJump: {
                url: '',
                type: '',// name或path ，决定用哪种方式跳转; router 只能识别name，axios只能识别path
            } //登陆后的跳转地址（用于身份过期后跳转到登陆页，登陆后再跳回原页面）
        }
    },
    getters: {
        getAdmin(): Object {
            return this.admin
        },
        getRules(): Object {
            return this.rules
        },
        getRuleTree(): Object {
            return this.rulesTree
        },
        getDepts(): Object {
            return this.depts
        },
        getDeptsTree(): Object {
            return this.deptsTree
        },
        getLoginJump(): ILoginJump {
            return this.loginJump
        }
    },
    actions: {
        setAdmin(res: Object): void {
            this.setDeptsTree(res.deptTree)
            this.setDepts(res.owenDepts)
            this.setRulesTree(res.rulesTree)
            this.setRules(res.rules)
            delete res.rulesTree
            delete res.rules
            delete res.deptTree
            delete res.owenDepts
            this.admin = res
        },
        setDeptsTree(res: Object): void {
            this.deptsTree = res
        },
        setDepts(res): void {
            this.depts = res
        },
        setRules(res: Object): void {
            this.rules = res
        },
        setRulesTree(res): void {
            this.rulesTree = res
        },
        setLoginJump(loginJump: ILoginJump): void {
            this.loginJump = loginJump
        },
        delLoginJump(): void {
            this.loginJump = {
                url: '',
                type: '',
            }
        },
        delAdmin() {
            this.admin = {}
            this.delRulesTree()
            this.delRules()
            this.detDeptsTree()
            this.detDepts()
        },
        delRulesTree() {
            this.rulesTree = []
        },
        delRules() {
            this.rules = {}
        },
        detDeptsTree() {
            this.deptsTree = []
        },
        detDepts() {
            this.depts = {}
        }
    },
    persist: true
})